<?php

class Pictures_Image_EditAction extends BlogPicturesBaseAction
{
    /**
     * Process HTTP POST request
     *
     * @param AgaviRequestDataHolder $rd
     * @return string
     */
    public function executeWrite(AgaviRequestDataHolder $rd)
    {
        /* @var $model Pictures_PicturesModel */
        $model = $this->getContext()->getModel('Pictures', 'Pictures');
        $vm = $this->getContainer()->getValidationManager();
		$params = $rd->getParameters();
		$new_file = $rd->getFile('image');
		
		$picture = $model->getPicture($params['pic_id'], null);
		if ($picture->name === $params['image_name']) {
		    $params['image_name'] = null;
		}
		
		try {
	        $model->updatePicture($picture, $new_file, $params['image_name']);
	    } catch (WereWordFilesystemException $e) {
	        $vm->setError('general', sprintf('Filesystem error: %s.', $e->getMessage()));
	        return 'Error';
	    }
	    
	    return 'Success';
    }
    
    public function executeRead(AgaviRequestDataHolder $rd)
    {
        return 'Input';
    }
    
	/**
	 * Returns the default view if the action does not serve the request
	 * method used.
	 *
	 * @return     mixed <ul>
	 *                     <li>A string containing the view name associated
	 *                     with this action; or</li>
	 *                     <li>An array with two indices: the parent module
	 *                     of the view to be executed and the view to be
	 *                     executed.</li>
	 *                   </ul>
	 */
	public function getDefaultViewName()
	{
		return 'Input';
	}
}

?>
